package com.tianze.library.adapter.pager;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * Created by kingtami.
 */
public class DestroyItemFragmentPagerAdapter extends FragmentStatePagerAdapter {

    Context context;
    IPageFragment[] pages;
    Bundle args;

    public DestroyItemFragmentPagerAdapter(Context context, FragmentManager fm, IPageFragment[] pages) {
        super(fm);
        this.context = context;
        this.pages = pages;
    }

    public DestroyItemFragmentPagerAdapter(Context context, Bundle agrs, FragmentManager fm, IPageFragment[] pages) {
        this(context, fm, pages);
        this.args = agrs;
    }

    @Override
    public Fragment getItem(int position) {
        try {
            Fragment fragment = (Fragment) pages[position].getClz().newInstance();
            if (args != null)
                fragment.setArguments(args);
            return fragment;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount() {
        return pages.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return context.getString(pages[position].getResName());
    }
}